﻿using ConfExpert.Forms.Base;

using DevExpress.Utils;
using DevTeam;

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Kauf
{
    public partial class StokDurumu : XForm
    {
        int CustomerId = 0;
        public StokDurumu(int _CustomerId)
        {
            InitializeComponent();
            CustomerId = _CustomerId;
        }


        BindingSource bs, bsSip, bsTop;
        private void StokDurumu_Load(object sender, EventArgs e)
        {

            //ds.xKaufihtiyac.Columns.Add("Blocked", typeof(float), "SUM(Child(FK_xKaufihtiyacSip_xKaufihtiyac_ProductId).BlockedQty)");
            ds.xKaufihtiyacTop.Columns.Add("En", typeof(decimal));
            ds.xKaufihtiyacSip.Columns.Add("TahminiBM", typeof(decimal));
            bs = new BindingSource(ds, "xKaufihtiyac");
            bsSip = new BindingSource(bs, "FK_xKaufihtiyacSip_xKaufihtiyac_ProductId");
            bsTop = new BindingSource(bs, "FK_xKaufihtiyacTop_xKaufihtiyac_ProductId");

            InitGridView(gridView1, bs);
            InitGridView(gridView2, bsSip);
            InitGridView(gridView3, bsTop);

            gridView1.MakeReadOnly();
            gridView2.MakeReadOnly();
            gridView3.MakeReadOnly();

            gridView2.MakeColumnInvisible("Scan", "ProductId");
            gridView3.MakeColumnInvisible("Scan", "ProductId");

            gridView2.MakeColumnInvisible("Tip", "PA", "Label");

            gridView1.AddSummary("QtyNeeded", "QtyDepo", "BlockedQty", "AvailQty");
            gridView2.AddSummary("QtyNeeded", "ProdPrice", "BlockedQty", "TotalPerPlan");
            gridView3.AddSummary("Qty");

            gridView2.YanYanaGetir("TahminiBM:TotalPerPlan");

            gridView1.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gridView1_RowCellStyle);
            gridView2.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gridView1_RowCellStyle);
            gridView3.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gridView1_RowCellStyle);

            splitContainerControl1.AutoSize();
            splitContainerControl2.AutoSize();

            bs.CurrentChanged += new EventHandler(bs_CurrentChanged);
            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            btnGuncelle.PerformClick();

            GridManager gm = GridManager.GetManager(gridView1);
            gm.GridMenu.AddMenu("Bu artikelin HAREKETLERİ", HareketleriGoster);

            gridView1.FormatAsNumber(1, "QtyNeeded", "QtyDepo", "BlockedQty", "AvailQty");
            gridView2.FormatAsNumber(1, "QtyNeeded", "BlockedQty", "ProdPrice");
            gridView2.FormatAsNumber(2, "TahminiBM");
            gridView3.FormatAsNumber(1, "Qty");
        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ds.Clear();
            GetDetails("xKaufihtiyac;xKaufihtiyacSip;xKaufihtiyacTop", "GetKaufihtiyac", memoExEdit2.EditValue.ToStr(), CustomerId);
            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
            gridView3.BestFitColumns();
        }


        void HareketleriGoster(object sender, EventArgs e)
        {
            int productId = bs.GetCurrentValue("ProductId").ToInt(-1);
            if (productId > 0)
                using (DepoStokHareketler rzv = new DepoStokHareketler(productId))
                {
                    rzv.ShowDialog();
                }
        }


        void bs_CurrentChanged(object sender, EventArgs e)
        {
            gridView2.BestFitColumns();
        }


        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "MusteriKodu" || e.Column.FieldName == "Qty")
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }
    }
}